import {
	getBookDetail
} from "@/api/api";
import {
	getCate
} from "@/utils/getCate";
import {
	globalRouteGuard
} from "@/utils/auth";

Page({
	data: {
		tabBarHeight: 105,
		mainActiveIndex: 0,
		activeId: null,
		categoryList: [],
		loading: false // 添加加载中状态标记
	},
	async onLoad() {
		// 使用全局路由守卫检查登录状态
		if (!globalRouteGuard('/pages/category/category')) {
			return;
		}
		
		// 显示加载中提示
		this.setData({
			loading: true
		});
		wx.showLoading({
			title: '加载分类中...',
		});
		
		try {
			await getCate();
			const app = getApp();
			this.setData({
				categoryList: app.categoryList || []
			});
		} catch (error) {
			console.log(error || '获取分类失败');
			wx.showToast({
				title: '获取分类失败',
				icon: 'none'
			});
		} finally {
			// 隐藏加载中提示
			this.setData({
				loading: false
			});
			wx.hideLoading();
		}
	},
	onClickNav({
		detail = {}
	}) {
		this.setData({
			mainActiveIndex: (detail)?.index || 0,
		});
	},

	onClickItem({
		detail = {}
	}) {
		const activeId = this.data.activeId === (getBookDetail)?.id ? null : (detail)?.id;
		console.log("detail", detail, activeId);
		this.setData({
			activeId
		});
		//跳转到对应的分类页面
		wx.navigateTo({
			url: './cateDetail/cateDetail?id=' + activeId,
		})
	}
});